package no.kolstad.movies.model;
/**
 *  Copyright [2008] Christopher Kolstad
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
 * Unless required by applicable law or agreed to in writing, software distributed under the License 
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License. 
 */
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.*;


import org.springframework.security.GrantedAuthority;
import org.springframework.security.userdetails.UserDetails;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@NamedQueries( {
  @NamedQuery(name = "user.byName", query = "SELECT u FROM User u WHERE u.username = :name")
})
public class User extends BaseModelObject implements UserDetails, Serializable {

  private Long id;
  private String username;
  private String password;
  private String fullName;
  private String email;
  private Boolean activeflag;
  private Long version;
  private Set<Role> roles;
 /* private Set<Movie> movieCollection = new HashSet<Movie>();
  

  @OneToMany(cascade = CascadeType.ALL)
  @org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
  @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
  public Set<Movie> getMovieCollection() {
    return movieCollection;
  }

  public void setMovieCollection(Set<Movie> coll) {
    this.movieCollection = coll;
  }
*/
  @Id
  @GeneratedValue
  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  @Column(unique = true)
  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  @Column(unique = true)
  public String getEmail() {
      return email;
  }
  public void setEmail(String email) {
      this.email = email;
  }
  
  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public String getFullName() {
    return fullName;
  }

  public void setFullName(String fullName) {
    this.fullName = fullName;
  }

  public Boolean getActiveflag() {
    return activeflag;
  }

  public void setActiveflag(Boolean activeflag) {
    this.activeflag = activeflag;
  }

  @Version
  public Long getVersion() {
    return version;
  }

  public void setVersion(Long version) {
    this.version = version;
  }

  @ManyToMany
  @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
  public Set<Role> getRoles() {
    return roles;
  }

  public void setRoles(Set<Role> roles) {
    this.roles = roles;
  }
/*
  @Transient
  public boolean hasMovie(Movie m) {
    return movieCollection.contains(m);
  }
*/
  // ACEGI methods (UserDetails)

  // The following two are already present above
  // String getUsername();
  // String getPassword();

  @Transient
  public GrantedAuthority[] getAuthorities() {
    return getRoles().toArray(new GrantedAuthority[getRoles().size()]);
  }

  @Transient
  public boolean isAccountNonExpired() {
    return true;
  }

  @Transient
  public boolean isAccountNonLocked() {
    return true;
  }

  @Transient
  public boolean isCredentialsNonExpired() {
    return true;
  }

  @Transient
  public boolean isEnabled() {
    return getActiveflag().booleanValue();
  }
}
